//
//  EPCWelcomeView.swift
//  Weibo
//
//  Created by LioWu on 11/04/2017.
//  Copyright © 2017 MyGit. All rights reserved.
//

import UIKit
import SDWebImage

class EPCWelcomeView: UIView {
    
    @IBOutlet weak var iconImage: UIImageView!
    
    @IBOutlet weak var tipLabel: UILabel!
    
    @IBOutlet weak var avaterBottomCons: NSLayoutConstraint!
    
    class func welcomeView() -> EPCWelcomeView {
        
        let nib = UINib.init(nibName: "EPCWelcomeView", bundle: nil)
        
        let v = nib.instantiate(withOwner: nil, options: nil)[0] as! EPCWelcomeView
        
        v.frame = UIScreen.main.bounds
        
        return v
    }
    
    override func awakeFromNib() {
        
        guard let avaterUrlStr = EPCNetworking.shareManager.userAccount.avatar_large,
        let url = URL.init(string: avaterUrlStr) else {
            return
        }
        
        iconImage.sd_setImage(with: url, placeholderImage: UIImage.init(named: "avatar_default_big"))
        
        iconImage.layer.cornerRadius = iconImage.bounds.width * 0.5
        iconImage.layer.masksToBounds = true
    }
    
    override func didMoveToWindow() {
        super.didMoveToWindow()
        
        self.layoutIfNeeded()
        
        avaterBottomCons.constant = bounds.size.height - 200
        
        UIView.animate(withDuration: 2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: [], animations: {
            
            self.layoutIfNeeded()
            
        }) { (_) in
            
            UIView.animate(withDuration: 1, animations: {
                
                self.tipLabel.alpha = 1
                
            }, completion: { (_) in
                
                
                UIView.animate(withDuration: 1, animations: {
                    
                    self.alpha = 0
                    
                }, completion: { (_) in
                    
                    self.removeFromSuperview()
                })
            })
        }
    }

}
